import { dayjs } from 'element-plus'

//传入当前时间 返回 yyyy-mm-dd格式 日期 num？
export function formatTime(time,num){
    let time1 = null;
    if(time.toString().length <= 10) time1 = new Date(time*1000);
    else time1 = new Date(time);
                                                                                //formatVideoSpeedTime(time1.getDate() - num) num不知其作用
    return time1.getFullYear()+'-'+formatVideoSpeedTime(time1.getMonth()+1)+'-'+formatVideoSpeedTime(time1.getDate())+' '+formatVideoSpeedTime(time1.getHours())+':'+formatVideoSpeedTime(time1.getMinutes())
}
//传入时间返回 xx/0x 格式
export function formatVideoSpeedTime(time){
    let time1 = time;
    let time2 = null;
    if(time < 10) time1 = "0" + time
    if(time > 60){
        time2 = Math.floor(time / 60)
        if( Math.floor(time / 60) < 10 ) time2 = "0"+ Math.floor(time / 60)
        time1 = Math.floor(time1 % 60) 
        if( Math.floor(time % 60) < 10 ) time1 = "0"+ Math.floor(time % 60)
        return time2+":"+time1
    }
    return time1
    
}
/**
 * 
 * @param {时间戳} time 
 * @returns yyyy-mm-dd
 */
export function formatYMD(time){
    let year = dayjs(time).$y
    let month = (dayjs(time).$M + 1 ) < 10? '0' + (dayjs(time).$M + 1 ) : (dayjs(time).$M + 1 )
    let day = dayjs(time).$D < 10? '0' + dayjs(time).$D : dayjs(time).$D
    return year + '-' + month + '-' + day 
}
//time: 时间戳 返回周几+xx：xx（时间）
export function weekData(time){
    let week = dayjs.unix(time)
    let weekData = null
    if (week.$W !== 0) {
       weekData = '周' + new Intl.NumberFormat("zh-Hans-CN-u-nu-hanidec").format(week.$W)
    } else {
        weekData = '周日'
    }
    let hours = week.$H < 10? '0' + week.$H : week.$H
    let min = week.$m < 9?'0' + week.$m : week.$m

    return weekData + '\xa0\xa0' + hours + ':' + min
}
/**
 * 返回 x-x 日期格式
 * @param {时间戳} time 
 */
export function formatMD(time){
    return dayjs.unix(time).$M + 1 + '-' + dayjs.unix(time).$D
}
/**
 * 判断是否时当前年份 是则不用返回年 不是则需要返回年份返回 x-x-x 日期格式
 * @param {时间戳} time 
 */
export function formatIsYMD(time){
    return (dayjs.unix(time).$y == dayjs().format('YYYY')? '' : dayjs.unix(time).$y + '-') + (dayjs.unix(time).$M + 1) + '-' + dayjs.unix(time).$D
}
/**
 * 
 * @param {时间戳} time 
 * 传入时间戳 查看周几
 */
export function formatWeek(time){
    let week = dayjs.unix(time)
    let weekData = null
    if (week.$W !== 0) {
       weekData = '周' + new Intl.NumberFormat("zh-Hans-CN-u-nu-hanidec").format(week.$W)
    } else {
        weekData = '周日'
    }
    return weekData
}
/**
 * 
 * @returns 返回今天现在的时间戳和今天0点的时间戳
 */
export function getToday(){
    let now = dayjs().$y + '-' + (dayjs().$M + 1 ) + '-' + dayjs().$D + ' ' + '00:00'
    let today = new Date(now).getTime()
    //let yesday = today - 86400000
    return {
        nowTime: new Date(dayjs()).getTime(),
        today
    }
}
/**
 * 
 * @returns 返回今天0点的时间戳和昨天0点的时间戳
 */
export function getYesday(){
    let now = dayjs().$y + '-' + (dayjs().$M + 1 ) + '-' + dayjs().$D + ' ' + '00:00'
    let today = new Date(now).getTime()
    let yesday = today - 86400000
    return {
        today,
        yesday
    }
}